package org.catdroid.bomba.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.catdroid.bomba.server.Constants;
import org.catdroid.bomba.server.GameManager;
import org.catdroid.bomba.server.bean.Bomb;
import org.catdroid.bomba.server.bean.Players.PlayerNotFoundException;

import com.google.gson.Gson;

@SuppressWarnings("serial")
public class BombServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		Gson gson = new Gson();
		GameManager gm = new GameManager();
		resp.setContentType("application/json");
		resp.getWriter().println(gson.toJson(gm.getBomb()));
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String from = req.getParameter("from");
		String to = req.getParameter("to");
		String time = req.getParameter("time");
		int score = Integer.parseInt(req.getParameter("score"));

		Bomb bomb = new Bomb(from, to,time);
		GameManager gm = new GameManager();
		try {
			gm.setPlayerScore(from, score);
			gm.setBomb(bomb);
			resp.setContentType("application/json");
			resp.getWriter().println(Constants.JSON_OK);
		} catch (PlayerNotFoundException e) {
			resp.setContentType("application/json");
			resp.getWriter().println(Constants.JSON_KO);
		}
	

	}
}
